//
//  LGCareTemplateDiseaseCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/19.
//  照护计划-模版导入-共病Cell

#import "LGCareTemplateDiseaseCell.h"

#define itemHeight  viewPix(32)

@interface LGCareTemplateDiseaseCell()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;

@end

@implementation LGCareTemplateDiseaseCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.baseView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.equalTo(self.contentView).offset(-viewPix(16));
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.bottom.equalTo(self.contentView).offset(-viewPix(8));
            make.height.equalTo(@(viewPix(112)));
        }];
    }
    return  self;
}

-(void)setItemArray:(NSArray *)itemArray{
    _itemArray = itemArray;
    for (UIButton *button in self.buttonArray) {
        [button removeFromSuperview];
    }
    [self.buttonArray removeAllObjects];
    CGFloat leftMargin = viewPix(10);
    CGFloat topMargin = viewPix(28);
    CGFloat minWdith = viewPix(100);
    CGFloat itemMargin = viewPix(8);
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSString *title = LGNSString(itemArray[i]);
        CGFloat textWidth = [title textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:MAXFLOAT]+viewPix(40);
        textWidth = (textWidth>minWdith)?textWidth:minWdith;
        topMargin = ((leftMargin+textWidth)>(Screen_W-viewPix(50)))?(topMargin+itemHeight+itemMargin):topMargin;
        leftMargin = ((leftMargin+textWidth)>(Screen_W-viewPix(50)))?viewPix(10):leftMargin;
        UIButton *button = [UIButton buttonWithTitle:title titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        button.frame = CGRectMake(leftMargin, topMargin, textWidth, itemHeight);
        button.cornerRidus = viewPix(16);
        [button addTarget:self action:@selector(itemButtonTouchedAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:button];
        [self.buttonArray addObject:button];
        leftMargin += textWidth+viewPix(8);
    }
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(topMargin+itemHeight+viewPix(12)));
    }];
    
}

-(void)setResultArray:(NSArray *)resultArray{
    _resultArray = resultArray;
    for (UIButton *button in self.buttonArray) {
        NSString *title = button.titleLabel.text;
        button.selected = ([resultArray containsObject:title])?YES:NO;
    }
}


-(void)itemButtonTouchedAction:(UIButton *)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(selectDisease:selected:)]){
        [self.delegate selectDisease:sender.titleLabel.text selected:!sender.selected];
    }
    sender.selected = !sender.selected;
}

-(NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:12];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
